package com.xsd.rabbitmq.routingkey;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.xsd.rabbitmq.util.ConnectionUtil;

/**
 * @Description: 消费者
 * @Author: zhoufuzong
 * @Created: 2021-03-14 13:58:13
 * @Version: 1.1.0
 */

public class Producer {
    // 交换机名称
    static final String  DIRECT_EXCHANGE="direct_exchange";
    // 队列名称
    static final String DIRECT_QUEUE_INSERT="direct_queue_insert";
    static final String DIRECT_QUEUE_UPDATE="direct_queue_update";


    public static void main(String[] args) throws Exception {

        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        /**
         * 声明交换机
         * 参数1: 交换机名称
         * 参数2： 交换机类型
         */
        channel.exchangeDeclare(DIRECT_EXCHANGE, BuiltinExchangeType.DIRECT);

        /**
         * 声明创建队列
         * 参数1：队列名称
         * 参数2：是否持久化队列
         * 参数3：是否独占本次连接
         * 参数4:是否在不使用时自动删除队列
         * 参数5：队列其他参数
         *
         */
        channel.queueDeclare(DIRECT_QUEUE_INSERT,true,false,false,null);
        channel.queueDeclare(DIRECT_QUEUE_UPDATE,true,false,false,null);

        // 队列绑定交换机
        channel.queueBind(DIRECT_QUEUE_INSERT,DIRECT_EXCHANGE,"insert");
        channel.queueBind(DIRECT_QUEUE_UPDATE,DIRECT_EXCHANGE,"update");

        String message =" hello routing-insert";
        /**
         * 参数1：交换机名
         * 参数2：路由key 简单模式可以传递队列名
         * 参数3：消息其他属性
         * 参数4：消息内容
         */
        channel.basicPublish(DIRECT_EXCHANGE,"insert",null,message.getBytes());
        System.out.println("已发送消息"+message);
        message= "hello routing-update";
        channel.basicPublish(DIRECT_EXCHANGE,"update",null,message.getBytes());
        System.out.println("已发送消息"+message);

      ConnectionUtil.closeConnection(channel,connection);

    }
}
